home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00076_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  11.0 KB  |  401 lines

  1. /*
  2.  * @(#)ImageMap.java    1.4 95/10/13  
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.applet.Applet;
  33. import java.awt.Image;
  34. import java.awt.Graphics;
  35. import java.awt.Rectangle;
  36. import java.util.StringTokenizer;
  37. import java.util.Vector;
  38. import java.util.Hashtable;
  39. import java.net.URL;
  40. import java.awt.image.*;
  41. import java.net.MalformedURLException;
  42.  
  43. /**
  44.  * An extensible ImageMap applet class.
  45.  * The active areas on the image are controlled by ImageArea classes
  46.  * that can be dynamically loaded over the net.
  47.  *
  48.  * @author     Jim Graham
  49.  * @version     1.4, 10/13/95
  50.  */
  51. public class ImageMap extends Applet implements Runnable {
  52.     /**
  53.      * The unhighlighted image being mapped.
  54.      */
  55.     Image baseImage;
  56.  
  57.     /**
  58.      * The list of image area handling objects;
  59.      */
  60.     ImageMapArea areas[];
  61.  
  62.     /**
  63.      * The primary highlight mode to be used.
  64.      */
  65.     static final int BRIGHTER = 0;
  66.     static final int DARKER = 1;
  67.  
  68.     int hlmode = BRIGHTER;
  69.  
  70.     /**
  71.      * The percentage of highlight to apply for the primary highlight mode.
  72.      */
  73.     int hlpercent = 50;
  74.  
  75.     /**
  76.      * Get a rectangular region of the baseImage highlighted according to
  77.      * the primary highlight specification.
  78.      */
  79.     Image getHighlight(int x, int y, int w, int h) {
  80.     return getHighlight(x, y, w, h, hlmode, hlpercent);
  81.     }
  82.  
  83.     /**
  84.      * Get a rectangular region of the baseImage with a specific highlight.
  85.      */
  86.     Image getHighlight(int x, int y, int w, int h, int mode, int percent) {
  87.     return getHighlight(x, y, w, h, new HighlightFilter(mode == BRIGHTER,
  88.                                 percent));
  89.     }
  90.  
  91.     /**
  92.      * Get a rectangular region of the baseImage modified by an image filter.
  93.      */
  94.     Image getHighlight(int x, int y, int w, int h, ImageFilter filter) {
  95.     Image cropped = makeImage(baseImage, new CropImageFilter(x, y, w, h));
  96.     return makeImage(cropped, filter);
  97.     }
  98.  
  99.     /**
  100.      * Make the primary highlighted version of the baseImage.
  101.      */
  102.     Image makeImage(Image orig, ImageFilter filter) {
  103.     return createImage(new FilteredImageSource(orig.getSource(), filter));
  104.     }
  105.  
  106.     /**
  107.      * Parse a string representing the desired highlight to be applied.
  108.      */
  109.     void parseHighlight(String s) {
  110.     if (s == null) {
  111.         return;
  112.     }
  113.     if (s.startsWith("brighter")) {
  114.         hlmode = BRIGHTER;
  115.         if (s.length() > "brighter".length()) {
  116.         hlpercent = Integer.parseInt(s.substring("brighter".length()));
  117.         }
  118.     } else if (s.startsWith("darker")) {
  119.         hlmode = DARKER;
  120.         if (s.length() > "darker".length()) {
  121.         hlpercent = Integer.parseInt(s.substring("darker".length()));
  122.         }
  123.     }
  124.     }
  125.  
  126.     /**
  127.      * Initialize the applet. Get attributes.
  128.      *
  129.      * Initialize the ImageAreas.
  130.      * Each ImageArea is a subclass of the class ImageArea, and is
  131.      * specified with an attribute of the form:
  132.      *         areaN=ImageAreaClassName,arguments...
  133.      * The ImageAreaClassName is parsed off and a new instance of that
  134.      * class is created.  The initializer for that class is passed a
  135.      * reference to the applet and the remainder of the attribute
  136.      * string, from which the class should retrieve any information it
  137.      * needs about the area it controls and the actions it needs to
  138.      * take within that area.
  139.      */
  140.     public void init() {
  141.     String s;
  142.  
  143.     parseHighlight(getParameter("highlight"));
  144.     introTune = getParameter("startsound");
  145.     baseImage = getImage(getDocumentBase(), getParameter("img"));
  146.     Vector areaVec = new Vector();
  147.     int num = 1;
  148.     while (true) {
  149.         ImageMapArea newArea;
  150.         s = getParameter("area"+num);
  151.         if (s == null) {
  152.         // Try rect for backwards compatibility.
  153.         s = getParameter("rect"+num);
  154.         if (s == null) {
  155.             break;
  156.         }
  157.         try {
  158.             newArea = new HighlightArea();
  159.             newArea.init(this, s);
  160.             areaVec.addElement(newArea);
  161.             String url = getParameter("href"+num);
  162.             if (url != null) {
  163.             s += "," + url;
  164.             newArea = new LinkArea();
  165.             newArea.init(this, s);
  166.             areaVec.addElement(newArea);
  167.             }
  168.         } catch (Exception e) {
  169.             System.out.println("error processing: "+s);
  170.             e.printStackTrace();
  171.             break;
  172.         }
  173.         } else {
  174.         try {
  175.             int classend = s.indexOf(",");
  176.             String name = s.substring(0, classend);
  177.             newArea = (ImageMapArea) Class.forName(name).newInstance();
  178.             s = s.substring(classend+1);
  179.             newArea.init(this, s);
  180.             areaVec.addElement(newArea);
  181.         } catch (Exception e) {
  182.             System.out.println("error processing: "+s);
  183.             e.printStackTrace();
  184.             break;
  185.         }
  186.         }
  187.         num++;
  188.     }
  189.     areas = new ImageMapArea[areaVec.size()];
  190.     areaVec.copyInto(areas);
  191.     checkSize();
  192.     }
  193.  
  194.     Thread aniThread = null;
  195.     String introTune = null;
  196.  
  197.     public void start() {
  198.     if (introTune != null)
  199.         try {
  200.         play(new URL(getDocumentBase(), introTune));
  201.         } catch (MalformedURLException e) {}
  202.     if (aniThread == null) {
  203.             aniThread = new Thread(this);
  204.             aniThread.setName("ImageMap Animator");
  205.             aniThread.start();
  206.     }
  207.     }
  208.  
  209.     public void run() {
  210.     Thread me = Thread.currentThread();
  211.     me.setPriority(Thread.MIN_PRIORITY);
  212.     for (int i = areas.length; --i >= 0; ) {
  213.         areas[i].getMedia();
  214.     }
  215.     while (aniThread == me) {
  216.         boolean animating = false;
  217.         for (int i = areas.length; --i >= 0; ) {
  218.         animating = areas[i].animate() || animating;
  219.         }
  220.         try {
  221.         synchronized(this) {
  222.             wait(animating ? 100 : 0);
  223.         }
  224.         } catch (InterruptedException e) {
  225.         break;
  226.         }
  227.     }
  228.     }
  229.  
  230.     public synchronized void startAnimation() {
  231.     notify();
  232.     }
  233.  
  234.     public synchronized void stop() {
  235.     aniThread = null;
  236.     notify();
  237.     }
  238.  
  239.     /**
  240.      * Check the size of this applet while the image is being loaded.
  241.      */
  242.     void checkSize() {
  243.     int w = baseImage.getWidth(this);
  244.     int h = baseImage.getHeight(this);
  245.     if (w > 0 && h > 0) {
  246.         resize(w, h);
  247.         synchronized(this) {
  248.         fullrepaint = true;
  249.         }
  250.         repaint(0, 0, w, h);
  251.     }
  252.     }
  253.  
  254.     private boolean fullrepaint = false;
  255.     private final static long UPDATERATE = 100;
  256.  
  257.     /**
  258.      * Handle updates from images being loaded.
  259.      */
  260.     public boolean imageUpdate(Image img, int infoflags,
  261.                    int x, int y, int width, int height) {
  262.     if ((infoflags & (WIDTH | HEIGHT)) != 0) {
  263.         checkSize();
  264.     }
  265.     if ((infoflags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0) {
  266.         synchronized(this) {
  267.         fullrepaint = true;
  268.         }
  269.         repaint(((infoflags & (FRAMEBITS | ALLBITS)) != 0)
  270.             ? 0 : UPDATERATE,
  271.             x, y, width, height);
  272.     }
  273.     return (infoflags & (ALLBITS | ERROR)) == 0;
  274.     }
  275.  
  276.     /**
  277.      * Paint the image and all active highlights.
  278.      */
  279.     public void paint(Graphics g) {
  280.     synchronized(this) {
  281.         fullrepaint = false;
  282.     }
  283.     if (baseImage == null) {
  284.         return;
  285.     }
  286.     g.drawImage(baseImage, 0, 0, this);
  287.     if (areas != null) {
  288.         for (int i = areas.length; --i >= 0; ) {
  289.         areas[i].highlight(g);
  290.         }
  291.     }
  292.     }
  293.  
  294.     /**
  295.      * Update the active highlights on the image.
  296.      */
  297.     public void update(Graphics g) {
  298.     boolean full;
  299.     synchronized(this) {
  300.         full = fullrepaint;
  301.     }
  302.     if (full) {
  303.         paint(g);
  304.         return;
  305.     }
  306.     if (baseImage == null) {
  307.         return;
  308.     }
  309.     g.drawImage(baseImage, 0, 0, this);
  310.     if (areas == null) {
  311.         return;
  312.     }
  313.     // First unhighlight all of the deactivated areas
  314.     for (int i = areas.length; --i >= 0; ) {
  315.         areas[i].highlight(g);
  316.     }
  317.     }
  318.  
  319.     /**
  320.      * Make sure that no ImageAreas are highlighted.
  321.      */
  322.     public void mouseExit() {
  323.     for (int i = 0; i < areas.length; i++) {
  324.         areas[i].exit();
  325.     }
  326.     }
  327.  
  328.     /**
  329.      * Find the ImageAreas that the mouse is in.
  330.      */
  331.     public boolean mouseMove(java.awt.Event evt, int x, int y) {
  332.     boolean eaten = false;
  333.  
  334.     for (int i = 0; i < areas.length; i++) {
  335.         if (!eaten && areas[i].inside(x, y)) {
  336.         eaten = areas[i].checkEnter(x, y);
  337.         } else {
  338.         areas[i].checkExit();
  339.         }
  340.     }
  341.  
  342.     return true;
  343.     }
  344.  
  345.     int pressX;
  346.     int pressY;
  347.  
  348.     /**
  349.      * Inform all active ImageAreas of a mouse press.
  350.      */
  351.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  352.     pressX = x;
  353.     pressY = y;
  354.  
  355.     for (int i = 0; i < areas.length; i++) {
  356.         if (areas[i].inside(x, y)) {
  357.         if (areas[i].press(x, y)) {
  358.             break;
  359.         }
  360.         }
  361.     }
  362.  
  363.     return true;
  364.     }
  365.  
  366.     /**
  367.      * Inform all active ImageAreas of a mouse release.
  368.      * Only those areas that were inside the original mouseDown()
  369.      * are informed of the mouseUp.
  370.      */
  371.     public boolean mouseUp(java.awt.Event evt, int x, int y) {
  372.     for (int i = 0; i < areas.length; i++) {
  373.         if (areas[i].inside(pressX, pressY)) {
  374.         if (areas[i].lift(x, y)) {
  375.             break;
  376.         }
  377.         }
  378.     }
  379.  
  380.     return true;
  381.     }
  382.  
  383.     /**
  384.      * Inform all active ImageAreas of a mouse drag.
  385.      * Only those areas that were inside the original mouseDown()
  386.      * are informed of the mouseUp.
  387.      */
  388.     public boolean mouseDrag(java.awt.Event evt, int x, int y) {
  389.     mouseMove(evt, x, y);
  390.     for (int i = 0; i < areas.length; i++) {
  391.         if (areas[i].inside(pressX, pressY)) {
  392.         if (areas[i].drag(x, y)) {
  393.             break;
  394.         }
  395.         }
  396.     }
  397.  
  398.     return true;
  399.     }
  400. }
  401.